// pages/bindphone/bindphone.js
var util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    'phone': '',
    'disabled': false,
    'binddisabled': true,
    'time': '获取',
    'currentTime': 60,
     'offline': false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  validcode: function (e) {
    const that=this;
    const phone = e.detail.value.phone;
    const code = e.detail.value.code;
    const accountid = app.globalData.accountid;
    const url = "/apip/validsecuritycode/" + phone + "/" + code;
    const data = { "accountid": accountid, "phone": phone }
    util.DoCommand(url, null, data, function (resultdata) {
      console.log(resultdata)
        util.ShowMessage("VPI会员用户绑定成功!", "success")
        app.globalData.tel=phone;//手机号
        app.globalData.accounttype = 1;//账户类型
        app.globalData.patientid = resultdata.id;//患者id
        wx.navigateBack({
          delta: 1
        })
     }, function (data) {
      util.ShowMessage(data.msg);
      return false;
      }, that)
  },
  sendmsg: function () {
    var that = this;
    var currentTime = that.data.currentTime;
    const phone = that.data.phone;
    if(!this.validatemobile(phone)){
      return false;
    }
    const url = "/apic/getsecuritycode/" + phone;
    util.DoCommand(url, null, null, function (data) {
        util.ShowMessage(data, "success")
    },function(data){
       util.ShowMessage(data.msg)
       that.setData({
         time: '获取',
         currentTime: 60,
         disabled: false,
         binddisabled: true
       })
       return;
    },that)
    that.setData({
      time: currentTime + '秒',
      disabled: true,
      binddisabled: false
    })
    var interval = setInterval(function () {
      that.setData({
       time: (currentTime - 1) + '秒'
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '获取',
          currentTime:60,
          disabled: false,
          binddisabled: true
        })
      }
    }, 1000)
  },
  bindKeyInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  }, 
  //验证手机号
  validatemobile: function (mobile) {
    if (mobile.length == 0) {
      util.ShowMessage('请输入手机号！')
      return false;
    }
    if (mobile.length != 11) {
      util.ShowMessage('手机号长度有误！')
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      util.ShowMessage('手机号有误！')
      return false;
    }
    return true;
  }

})